//
//  PeekPagedScrollViewController.m
//  ScrollViews
//
//  Created by cygarm on 15/9/4.
//  Copyright (c) 2015年 cygarm. All rights reserved.
//

#import "PeekPagedScrollViewController.h"

@interface PeekPagedScrollViewController ()

@property (nonatomic, strong) NSArray *pageImages;
@property (nonatomic, strong) NSMutableArray *pageViews;

- (void)loadVisiblePages;
- (void)loadPage:(NSInteger)page;
- (void)purgePage:(NSInteger)page;

@end

@implementation PeekPagedScrollViewController

- (void)viewDidLoad {
	[super viewDidLoad];
	
		// 1
	self.pageImages = [NSArray arrayWithObjects:
										 [UIImage imageNamed:@"photo1"],
										 [UIImage imageNamed:@"photo2"],
										 [UIImage imageNamed:@"photo3"],
										 [UIImage imageNamed:@"photo4"],
										 [UIImage imageNamed:@"photo5"],
										 nil];
	NSInteger pageCount = self.pageImages.count;
	
		// 2
	self.pageControl.currentPage = 0;
	self.pageControl.numberOfPages = pageCount;
	
		// 3
	self.pageViews = [[NSMutableArray alloc] init];
	for (NSInteger i = 0; i < pageCount; ++ i) {
		[self.pageViews addObject:[NSNull null]];
	}
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
 
		// Set up the content size of the scroll view
	CGSize pagesScrollViewSize = self.scrollView.frame.size;
	self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
 
		// Load the initial set of pages that are on screen
	NSLog(@"viewWillAppear");
	[self loadVisiblePages];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)loadPage:(NSInteger)page {
	if (page < 0 || page >= self.pageImages.count) {
		return;
	}
	NSLog(@"loadPage:%ld, pageTotal:%ld", (long)page, self.pageImages.count);

		// 1
	UIView *pageView = [self.pageViews objectAtIndex:page];
	if ((NSNull *)pageView == [NSNull null]) {
			// 2
		CGRect frame = self.scrollView.bounds;
		NSLog(@"frame origin=%@", NSStringFromCGRect(frame));

		frame.origin.x = frame.size.width * page;
		frame.origin.y = 0.0f;
		frame = CGRectInset(frame, 10.0f, 0.0f);
		NSLog(@"frame CGRectInset=%@", NSStringFromCGRect(frame));
		
			// 3
		UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
		newPageView.contentMode = UIViewContentModeScaleAspectFit;
		newPageView.frame = frame;
		[self.scrollView addSubview:newPageView];
		
			// 4
		[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
	}
}

- (void)purgePage:(NSInteger)page {
	if (page < 0 || page >= self.pageImages.count) {
		return;
	}
	NSLog(@"purgePage:%ld, pageTotal:%ld", (long)page, self.pageImages.count);

		// Remove a page from the scroll view and reset the container array
	UIView *pageView = [self.pageViews objectAtIndex:page];
	if ((NSNull *)pageView != [NSNull null]) {
		[pageView removeFromSuperview];
		[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
	}
}

- (void)loadVisiblePages {
		// First, determine which page is currently visible
	CGFloat pageWidth = self.scrollView.frame.size.width;
	NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
	
		// Update the page control
	self.pageControl.currentPage = page;
	
		// Work out which pages you want to load
	NSInteger firstPage = page - 1;
	NSInteger lastPage = page + 1;
	
		// Purge anything before the first page
	for (NSInteger i = 0; i < firstPage; ++ i) {
		[self purgePage:i];
	}
	
		// Load pages in our range
	for (NSInteger i = firstPage; i <= lastPage; ++ i) {
		[self loadPage:i];
	}
	
		// Purge anything after the last page
	for (NSInteger i = lastPage + 1; i < self.pageImages.count; ++ i) {
		[self purgePage:i];
	}
}

#pragma mark - UIScrollView delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {	NSLog(@"scrollViewDidScroll");
	[self loadVisiblePages];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
